﻿using SdmxMl.Common;
using SdmxMl.Structure;

namespace SakeVar
{
    /// <summary>
    /// Internalisation support for Group information and Type of variables.
    /// Based on codelists defining the translations
    /// </summary>
    public class GroupVarHelper
    {
        #region Constants

        private const string AbbrevType = "ABBREV";

        #endregion

        #region Internal Members

        private CodeListType clGroupPers;
        private CodeListType clVarType;

        #endregion

        #region Constructor

        /// <summary>Constructor holds codelists used for internalisation</summary>
        /// <param name="cltGrp">Group translation codelist</param>
        /// <param name="cltVar">Var type translation codelist</param>
        public GroupVarHelper(CodeListType cltGrp, CodeListType cltVar)
        {
            clGroupPers = cltGrp;
            clVarType = cltVar;
        }

        #endregion

        #region public Methods

        /// <summary>Provides group text in current language </summary>
        /// <param name="groupCode">id of group</param>
        /// <returns>Text associated in current language</returns>
        public string GetTextOfGroup(string groupCode)
        {
            string txt = string.Empty;
            ISdmxHierarchyListItem item = clGroupPers.CodeList.LocateItem(groupCode);
            if (item != null)
                txt = item.Name.ToString();

            return txt;
        }

        /// <summary>Provides text associated to VarType </summary>
        /// <returns>text in curent language</returns>
        public string GetVarTypeTooltip()
        {
            return clVarType.Name.ToString();
        }

        /// <summary>Provides text associated to group code id </summary>
        /// <returns>text in curent language</returns>
        public string GetTranslationOfGroup(string groupCode)
        {
            string txt = string.Empty;
            ISdmxHierarchyListItem item = clGroupPers.CodeList.LocateItem(groupCode);
            if (item != null)
            {
                Annotation a = item.AnnotationList.GetAnnotationType(AbbrevType);
                if (a != null)
                    txt = a.Text.ToString();
            }
            return txt;
        }

        /// <summary>Provides text associated to VarType current id </summary>
        /// <returns>text in curent language</returns>
        public string GetTooltypOfVartype(string cellContent)
        {
            string txt = string.Empty;

            string[] arr = cellContent.Split('+');
            foreach (string t in arr)
            {
                foreach (CodeType c in clVarType.CodeList)
                {
                    Annotation a = c.AnnotationList.GetAnnotationType(AbbrevType);
                    if (a != null && a.Text.ToString().Trim() == t)
                    {
                        if (txt.Length > 0)
                            txt += " + ";
                        txt += c.Name.ToString();
                    }
                }
            }
            return txt;
        }

        private string GetTranslationOfVarType(string typeCode)
        {
            string txt = string.Empty;
            ISdmxHierarchyListItem item = clVarType.CodeList.LocateItem(typeCode);
            if (item != null)
            {
                Annotation a = item.AnnotationList.GetAnnotationType(AbbrevType);
                if (a != null)
                    txt = a.Text.ToString();
            }
            return txt;
        }

        public string GetTranslationOfVarTypeGroup(string typeGroup)
        {
            string txt = string.Empty;
            string[] arr = typeGroup.Split('+');
            for(int i = 0; i < arr.Length; i++)
            {
                if (i > 0)
                    txt += '+';
                txt += GetTranslationOfVarType(arr[i].Trim());
            }

            return txt;
        }
        #endregion
    }
}
